<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>赖俊杰</title>
    <link rel="stylesheet" href="../css/reset.css" media="screen"
          title="no title" charset="utf-8">
    <link rel="stylesheet" href="css/master.css" media="screen" title="no title"
          charset="utf-8">
</head>
<body>
<header>
    <nav role="navigation">
        <ul class="main-nav">
        	<li><a href="../index.html">Home</a></li>
            <li><a href="">Adminimize</a></li>
            <li><a href="new.html">Add-New</a></li>
        </ul>
    </nav>
</header>
<main role="main">
    <section class="list-cart">
        <table class="article-list">
            <caption>管理文章</caption>
            <tbody class="article-list-body">
                <tr class="article-head">
                    <th>标题</th>
                    <th>日期</th>
                    <th>标签</th>
                    <th colspan="2">操作</th>
                </tr>
            </tbody>
        </table>
    </section>
</main>
<footer>
</footer>
<script type="text/javascript" src="../js/jquery-1.12.4.min.js"></script>
<script>

/**
 * 获取表格数据
 */
function getArtList(){
	var count = 1;
	$.ajax({
		url : "../getList.do",
		type : "get",
		data : {
			"startNum" : 10*(count-1),
			"counted" : 10
		},
		dataType : "json",
		success : function(msg) {
			console.log(msg);
			var list = '';
			var length = msg["articles"].length;
			var art = msg["articles"];

			for (var i = 0; i < length; ++i) {
				var item ="<tr><td>"
						+art[i].title
						+"</td><td>"
						+art[i].publish_time
						+"</td><td>"
						+art[i].category
						+"</td><td class='deleBtn' value="+art[i].id+" >删除</td><td><a href='modify.html?id="+art[i].id+"'>修改</a></td></tr>";	
				list += item;
			}
			$(".article-list-body").append(list);
		},
		error : function(er) {
			console.log(er);
		}
	});
}

	$("docuemnt").ready(function(){
		getArtList();
	});
	
	/**
	*删除文章事件
	*/
	$("document").ready(function(){
		//删除所选文章
		$(".article-list-body").on("click",$('.deleBtn'),function(e){
			//判断点击的对象是否有"deleBtn"
			if($(e.target).hasClass("deleBtn")){
				var id = $(e.target).attr("value");
				$.ajax({
					url : "../delete.do",
					type : "POST",
					data : {
						"id" : id
					},
					dataType : "json",
					success : function(msg) {
						if(msg){
							alert("删除成功");
							$(e.target).parent().remove();
						}else{
							alert("删除失败");
						}
					},
					error : function(er) {
						console.log(er);
						alert("网络传输错误");
					}
				});	
			}
		});
	})
		
	
</script>
</body>
</html>